﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ThietBiSo.LinQDB;

namespace ThietBiSo.Models
{
    public class MTinTuc
    {
        private _24hmuaonlineDataContext context = new _24hmuaonlineDataContext();

        public List<TinTuc> FindAll()
        {
            var v = from f in context.TinTucs orderby f.MaTinTuc descending select f;
            return v.ToList();
        }


        public int GetTotal()
        {
            return FindAll().Count();
        }
        public int GetTotal_TrangChu()
        {
            return FindAll_TrangChu().Count();
        }


        public List<TinTuc> FindAll_TrangChu()
        {
            var v = from f in context.TinTucs.Where(d => d.HienThi == true) orderby f.MaTinTuc descending select f;
            return v.ToList();
        }       
       
        public List<TinTuc> FindAll_Web()
        {
            var v = from f in context.TinTucs.Where(d => d.TinMoi == true) orderby f.MaTinTuc descending select f;
            return v.ToList();
        }
        public List<TinTuc> GetList(int start, int offset)
        {
            var v = new List<TinTuc>();
            v = FindAll_Web().Skip(start).Take(offset).ToList();
            return v;
        }
        public TinTuc ChiTiet(int id)
        {
            var v = from f in context.TinTucs.Where(d => d.MaTinTuc == id) select f;
            return v.SingleOrDefault();
        }
        public string GetTitleByMa(int ma)
        {
            var v = from f in context.TinTucs.Where(d => d.MaTinTuc == ma) select f.TieuDe;
            return v.FirstOrDefault();
        }
        public int GetTotalTinTuc()
        {
            return FindAll().Count();
        }
        //public int GetTotalTinTuc(int id)
        //{
        //    return FindAll().Where(d => d.MaLoaiTinTuc == id).Count();
        //}
        //public int GetTotalTinTuc(string maloai)
        //{
        //    int total = 0;
        //    if (maloai == "" || maloai == "0" || maloai == null)
        //    {
        //        total = FindAll().Where(d=>d.MaLoaiTinTuc==1).Count();

        //    }
        //    else
        //    {
        //        total = FindAll(int.Parse(maloai)).Count();
        //    }
        //    return total;
        //}
        public void Save(TinTuc tt)
        {
            context.TinTucs.InsertOnSubmit(tt);
            context.SubmitChanges();
        }
        public void Update(TinTuc tt)
        {
            var v = from f in context.TinTucs
                    select f;
            TinTuc ttdb = v.SingleOrDefault(d => d.MaTinTuc.Equals(tt.MaTinTuc));
            ttdb.TieuDe = tt.TieuDe;
           // ttdb.MaLoaiTinTuc = tt.MaLoaiTinTuc;
            ttdb.NoiDungTomTat = tt.NoiDungTomTat;
            ttdb.ChiTiet = tt.ChiTiet;
            ttdb.UrlHinh = tt.UrlHinh;
            ttdb.ThuTu = tt.ThuTu;
            context.SubmitChanges();
        }
        public void TinMoi(int id)
        {
            var v = from f in context.TinTucs
                    select f;
            TinTuc spdb = v.SingleOrDefault(d => d.MaTinTuc.Equals(id));
            if (spdb.TinMoi == true)
            {
                spdb.TinMoi = false;
            }
            else spdb.TinMoi = true;
            context.SubmitChanges();
        }
        public void HienThi(int id)
        {
            var v = from f in context.TinTucs
                    select f;
            TinTuc spdb = v.SingleOrDefault(d => d.MaTinTuc.Equals(id));
            if (spdb.HienThi == true)
            {
                spdb.HienThi = false;
            }
            else spdb.HienThi = true;
            context.SubmitChanges();
        }

        public void Delete(int id)
        {
            var v = from p in context.TinTucs
                    where p.MaTinTuc.Equals(id)
                    select p;
            TinTuc spdb = v.SingleOrDefault();
            context.TinTucs.DeleteOnSubmit(spdb);
            context.SubmitChanges();
        }
        public TinTuc GetByID(int id)
        {
            return (FindAll().Where(d => d.MaTinTuc == id)).SingleOrDefault();
        }


        public int GetMax()
        {
            return (from t in context.TinTucs
                    orderby t.MaTinTuc descending
                    select t.MaTinTuc).FirstOrDefault();
        }
        public int? GetMaxTT()
        {
            return (from t in context.TinTucs
                    orderby t.ThuTu descending
                    select t.ThuTu).FirstOrDefault();
        }
    }
}